package com.cskaoyan.service.wx;

import com.cskaoyan.bean.MarketAddress;
import com.cskaoyan.bean.MarketAddressExample;
import com.cskaoyan.bean.bo.wx.WxAddressSaveBo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketAddressMapper;
import com.cskaoyan.utils.DateUtil;
import com.cskaoyan.utils.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author: zhang
 * @Date: 2022/11/22 17:53
 * @Since: jdk1.8
 */
@Service
public class WxAddressServiceImpl implements WxAddressService {
    @Autowired
    MarketAddressMapper marketAddressMapper;

    @Override
    public CommonData<MarketAddress> list(Integer userId) {
        List<MarketAddress> marketAddresses = marketAddressMapper.selectAddressesByUseId(userId);
        PageHelper.startPage(1, marketAddresses.size());
        PageInfo<MarketAddress> pageInfo = new PageInfo<>(marketAddresses);
        return CommonData.data(pageInfo);
    }

    @Override
    public MarketAddress detail(Integer id) {
        return marketAddressMapper.selectByPrimaryKey(id);
    }


    @Override
    public int save(Integer userId, WxAddressSaveBo wxAddressSaveBo) {
        MarketAddress address = new MarketAddress();
        address.setName(wxAddressSaveBo.getName());
        address.setUserId(userId);
        address.setTel(wxAddressSaveBo.getTel());
        address.setProvince(wxAddressSaveBo.getProvince());
        address.setCity(wxAddressSaveBo.getCity());
        address.setCounty(wxAddressSaveBo.getCounty());
        address.setAreaCode(wxAddressSaveBo.getAreaCode());
        address.setAddressDetail(wxAddressSaveBo.getAddressDetail());
        // 邮政编码暂用areaCode
        address.setPostalCode(wxAddressSaveBo.getAreaCode());
        address.setIsDefault(wxAddressSaveBo.getIsDefault());
        address.setDeleted(false);
        // 判断是新增还是修改
        MarketAddressExample marketAddressExample = new MarketAddressExample();
        MarketAddressExample.Criteria criteria = marketAddressExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        Integer id = wxAddressSaveBo.getId();
        if(!StringUtil.isEmpty(String.valueOf(id))){
            criteria.andIdEqualTo(id);
        }
        MarketAddress selectedAddress = marketAddressMapper.selectByPrimaryKey(id);
        Date date = DateUtil.getDate();
        // 设置时间插入数据库
        if(selectedAddress==null){
            address.setAddTime(date);
            address.setUpdateTime(date);
            return marketAddressMapper.insert(address);
        }
        // 修改数据库
        address.setUpdateTime(date);
        // 要设置主键
        address.setId(wxAddressSaveBo.getId());
        return marketAddressMapper.updateByPrimaryKeySelective(address);
    }

    @Override
    public int delete(String id) {
        MarketAddressExample marketAddressExample = new MarketAddressExample();
        MarketAddressExample.Criteria criteria = marketAddressExample.createCriteria();
        if (!StringUtil.isEmpty(id)) {
            criteria.andIdEqualTo(Integer.parseInt(id));
        }
        criteria.andDeletedEqualTo(false);
        // 获取地址
        List<MarketAddress> marketAddresses = marketAddressMapper.selectByExample(marketAddressExample);
        MarketAddress address = marketAddresses.remove(0);
        Date date = DateUtil.getDate();
        address.setUpdateTime(date);
        address.setDeleted(true);
        // 重置example
        marketAddressExample.clear();
        if (!StringUtil.isEmpty(id)) {
            criteria.andIdEqualTo(Integer.parseInt(id));
        }
        criteria.andDeletedEqualTo(true);
        return marketAddressMapper.updateByExample(address, marketAddressExample);
    }
}
